package com.xinbochuang.machine.framework.web.service;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xinbochuang.machine.admin.domain.system.SysRole;
import com.xinbochuang.machine.admin.domain.system.SysUser;
import com.xinbochuang.machine.admin.mapper.system.SysRoleMapper;
import com.xinbochuang.machine.admin.mapper.system.SysUserMapper;
import com.xinbochuang.machine.admin.service.system.ISysUserService;
import com.xinbochuang.machine.common.domain.LoginUser;
import com.xinbochuang.machine.common.enums.UserStatus;
import com.xinbochuang.machine.common.exception.BaseException;
import com.xinbochuang.machine.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户验证处理
 *
 * @author 黄晓鹏
 * @date 2020-09-08 14:34
 */
@Service
@Slf4j
public class UserDetailsServiceImpl implements UserDetailsService {

    @Resource
    private ISysUserService userService;

    @Resource
    private SysUserMapper userMapper;

    @Resource
    private SysPermissionService permissionService;

    @Resource
    private SysRoleMapper roleMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
//        SysUser user = userService.getOne(Wrappers.<SysUser>lambdaQuery().eq(SysUser::getUsername, username).or().eq(SysUser::getIdCard, username));
        SysUser user = userMapper.selectUserByUserName(username);
        if (StringUtils.isNull(user)) {
            log.info("登录用户：{} 不存在.", username);
            throw new UsernameNotFoundException("登录用户：" + username + " 不存在");
        } else if (UserStatus.NOCHECK.getCode().equals(user.getStatus())) {
            log.info("登录用户：{} 未审核.", username);
            throw new BaseException("对不起，您的账号：" + username + " 未审核");
        } else if (UserStatus.NOPASS.getCode().equals(user.getStatus())) {
            log.info("登录用户：{} 未审核通过.", username);
            throw new BaseException("对不起，您的账号：" + username + " 未审核通过");
        } else if (UserStatus.DISABLE.getCode().equals(user.getStatus())) {
            log.info("登录用户：{} 已被停用.", username);
            throw new BaseException("对不起，您的账号：" + username + " 已停用");
        } else if (UserStatus.DELETED.getCode().equals(user.getDelFlag())) {
            log.info("登录用户：{} 已被删除.", username);
            throw new BaseException("对不起，您的账号：" + username + " 已被删除");
        }
        List<String> roleIds = roleMapper.selectRoleListByUserId(user.getUserId());
        if (!roleIds.isEmpty()) {
            List<SysRole> roles = roleMapper.selectList(Wrappers.<SysRole>lambdaQuery().in(SysRole::getRoleId, roleIds));
            user.setRoles(roles);
            user.setRoleIds(Convert.toIntArray(roleIds));
        }

        return createLoginUser(user);
    }

    public UserDetails createLoginUser(SysUser user) {
        return new LoginUser(user, permissionService.getMenuPermission(user));
    }
}
